﻿#include "ui_findprotocoleditor.h"
#include "protocoleditor.h"

FindProtocolEditor::FindProtocolEditor(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FindProtocolEditor)
{
    ui->setupUi(this);
    InitFindPanel();
}

FindProtocolEditor::~FindProtocolEditor()
{
    delete ui;
}

void FindProtocolEditor::InitFindPanel()
{
    setWindowTitle(tr("数据查找"));
    setWindowIcon(QIcon(":/Icon/find.png"));
    ui->InputE->setPlaceholderText("输入要查找的数据...");

    ui->FindPro->setOrientation(Qt::Horizontal);
    ui->FindPro->setMinimum(0);
    ui->FindPro->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    QStringList sHeaderList;
    sHeaderList<<"<系统Id>"<<"<子系统Id>"<<"<数据信息>"<<"<协议类型>";
    ui->MsgTree->setHeaderLabels(sHeaderList);
    ui->MsgTree->horizontalScrollBar()->setHidden(true);
    ui->Btncheck->setCheckState(Qt::Unchecked);
    ui->MsgTree->setAlternatingRowColors(true);
    ui->MsgTree->setFont(QFont("Microsoft YaHei", 9));
}

void FindProtocolEditor::reset()
{
    ui->InputE->clear();
    ui->MsgTree->clear();
    ui->FindPro->setValue(0);
}

void FindProtocolEditor::UpdateFindProgressBar(int nVal)
{
    ui->FindPro->setValue(nVal);
}

void FindProtocolEditor::on_FindBtn_clicked()
{
    if(ui->InputE->text().isEmpty())
    {
        QMessageBox::critical(this, "错误", "查找数据不能为空!","确定");
        return;
    }
    ui->MsgTree->clear();
    QString sInputMsg = ui->InputE->text();

    QList<int> sysIdList;   //协议id集合
    sysIdList.clear();

    QFile file(ProtocolEditor::GetSysFilePath());
    if(!file.open(QFile::ReadWrite)) return;

    QDomDocument doc;
    if(!doc.setContent(&file))
    {
        file.close();
        return;
    }
    file.close();
    QDomElement root = doc.documentElement();
    if(!root.hasChildNodes()) return ;
    QDomNode node = root.firstChild();

    while(!node.isNull())
    {
        if(node.isElement())
        {
            QDomElement ment = node.toElement();
            sysIdList.append(ment.attribute("id").toInt());
            node = node.nextSibling();
        }
    }
    if(sysIdList.isEmpty())
    {
        UpdateFindProgressBar(100);
        return;
    }
    //只查询子系统协议
    int nSize = sysIdList.size();
    int nProgressVal = 0;

    for(int idx = 0; idx < nSize;++idx)
    {
        int nProgeVal =static_cast<int>( (++nProgressVal)/static_cast<double>(nSize) *100);
        UpdateFindProgressBar(nProgeVal);

        QString sPath = ProtocolEditor::GetSubFilePath()+QString::number(sysIdList.at(idx))+".xml";
        QFile file(sPath);
        if(!file.open(QFile::ReadWrite)) continue;

        QDomDocument doc;
        if(!doc.setContent(&file))
        {
            file.close();
            continue;
        }
        file.close();

        QDomElement root = doc.documentElement();
        if(!root.hasChildNodes()) continue ;
        QDomNode node = root.firstChild();
        QVector<FINDDATA> findData;
        findData.clear();
        while(!node.isNull())
        {
            if(node.isElement())
            {
                QDomElement ment = node.toElement();
                INT nSubId = ment.attribute("id").toInt();
                INT nType = ment.attribute("type").toInt();
                QString sInfo = ment.attribute("name");
                if(sInfo.contains(sInputMsg,Qt::CaseInsensitive))
                {
                    FINDDATA data;
                    data.nSubId = nSubId;
                    data.nType = nType;
                    data.sInfo = sInfo;
                    findData.push_back(std::move(data));
                }
                node = node.nextSibling();
            }
        }
        if(!findData.isEmpty())
        {
            QTreeWidgetItem* SysItems = new QTreeWidgetItem(ui->MsgTree);
            SysItems->setText(0," [ "+QString::number(sysIdList.at(idx))+" ]");
            for(int j = 0; j < findData.size();++j)
            {
                const auto& data = findData.at(j);
                QTreeWidgetItem *SubItem = new QTreeWidgetItem(SysItems,data.nType);
                SubItem->setText(1," [ "+QString::number(data.nSubId)+" ]");
                SubItem->setText(2,data.sInfo);
                QString sMsgType = Probutility::GetSystemType(static_cast<eSystemType>(data.nType));
                SubItem->setText(3,sMsgType);
                if(data.nType == Server)
                {
                    const QColor color = QColor(252,222,156);
                    SubItem->setBackgroundColor(3,color);
                }
                SysItems->addChild(SubItem);
            }
        }
    }
}

void FindProtocolEditor::on_QuitBtn_clicked()
{
    ui->InputE->clear();
    ui->MsgTree->clear();
    ui->FindPro->setValue(0);
    close();
}

void FindProtocolEditor::on_MsgTree_itemDoubleClicked(QTreeWidgetItem *item, int )
{
    if(nullptr == item) return;
    if(nullptr == item->parent()) return ;

    const auto& pParent = item->parent();
    QString SysData = pParent->text(0);
    if(SysData.isEmpty()) return;

    int nSysId = SysData.mid(3,SysData.size()-4).toInt();
    QString SubData = item->text(1);
    int nSubId =SubData.mid(3,SubData.size()-4).toInt();
    emit sendFindReult(nSysId,nSubId,item->type());
}
